package com.app.controller;
import com.app.model.SpecialityManageModel;
import com.app.model.TrainModel;
import com.app.model.TrainSpecialityModel;
import com.app.service.SpecialityManageService;
import com.app.service.TrainService;
import com.app.service.TrainSpecialityService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.TrainSpecialityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
//培训机构-专业管理
@RestController
@Controller
@RequestMapping("/jiuquan/trainSpeciality")
public class TrainSpecialityController {
    @Autowired
    private TrainSpecialityService trainSpecialityService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private SpecialityManageService specialityManageService;
    //查询列表
    @GetMapping("/getTrainSpecialityList")
    public Result getTrainSpecialityList(@RequestParam(defaultValue = "0") Integer page,
                                         @RequestParam(defaultValue = "10") Integer limit, Integer trainId,@RequestParam(required = false)Integer type) {
        TrainSpecialityVo trainSpecialityList = trainSpecialityService.getTrainSpecialityList(page, limit,trainId,type);
        return Result.success(trainSpecialityList);
    }
    //详情
    @GetMapping("/getTrainSpecialityById")
    public Result getTrainSpecialityById(Integer id) {
        TrainSpecialityModel trainSpeciality = trainSpecialityService.getById(id);
        //设置培训机构名称
        Integer specialityId = trainSpeciality.getSpecialityId();
        TrainModel trainModel = trainService.getById(trainSpeciality.getTrainId());
        trainSpeciality.setTrainIdText(trainModel.getTrainInstitution());
        //设置专业名称
        SpecialityManageModel specialityManageModel = specialityManageService.getById(specialityId);
        if(specialityManageModel!=null)
        {
            trainSpeciality.setSpecialityText(specialityManageModel.getName());
        }
        //设置培训类型名称 0 创业培训  1 技能培训
        Integer trainType = trainSpeciality.getTrainType();
        if (trainType == 1) {
            trainSpeciality.setTrainTypeText("创业培训");
        }
        if (trainType == 2) {
            trainSpeciality.setTrainTypeText("技能培训");
        }
        return Result.success(trainSpeciality);
    }
    //新增和修改
    @PostMapping("/addAndUpdateTrainSpeciality")
    public Result addAndUpdateTrainSpeciality(@RequestBody TrainSpecialityModel trainSpecialityModel) {
        TrainSpecialityModel trainSpeciality = trainSpecialityService.getById(trainSpecialityModel.getId());
        if (trainSpeciality != null) {
            trainSpecialityModel.setUpdateTime(CommonHelp.getNowTimestamp());
            trainSpecialityService.updateById(trainSpecialityModel);
            return Result.success("修改成功");
        } else {
            trainSpecialityModel.setCreateTime(CommonHelp.getNowTimestamp());
            trainSpecialityService.save(trainSpecialityModel);
            return Result.success("添加成功");
        }
    }
    //删除
    @GetMapping("/delTrainSpeciality")
    public Result delTrainSpeciality(Integer id) {
        trainSpecialityService.removeById(id);
        return Result.success("删除成功");
    }
    //小程序端查询列表
    @GetMapping("/getUserTrainSpecialityList")
    public Result getUserTrainSpecialityList(@RequestParam(defaultValue = "0") Integer page,
                                             @RequestParam(defaultValue = "10") Integer limit, Integer trainId) {
        TrainSpecialityVo userTrainSpecialityList = trainSpecialityService.getUserTrainSpecialityList(page, limit, trainId);
        return Result.success(userTrainSpecialityList);
    }
}
